#include "drv_usart.h"
void drv_usart6_init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	USART_InitTypeDef USART_InitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
	
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;//复用模式
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_14;
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOG, & GPIO_InitStruct);

	GPIO_PinAFConfig(GPIOG, GPIO_PinSource9, GPIO_AF_USART6);
	GPIO_PinAFConfig(GPIOG, GPIO_PinSource14, GPIO_AF_USART6);
	
	USART_InitStruct.USART_BaudRate = 115200;
	USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_InitStruct.USART_Parity = USART_Parity_No;//
	USART_InitStruct.USART_StopBits = USART_StopBits_1;
	USART_InitStruct.USART_WordLength = USART_WordLength_8b;
	USART_Init(USART6, &USART_InitStruct);
	
	USART_ITConfig(USART6, USART_IT_RXNE, ENABLE);//开启接收中断
	USART_ITConfig(USART6, USART_IT_IDLE, ENABLE);//开启空闲中断
	NVIC_InitStruct.NVIC_IRQChannel = USART6_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStruct);
	USART_ClearFlag(USART6, USART_FLAG_TC);//清空发送完成标志，否则会出现第一个字母丢失
	USART_Cmd(USART6, ENABLE);
}
int fputc(int ch,FILE *f)//重构fputc函数
{
	USART_SendData(USART6, ch);
	while(USART_GetFlagStatus(USART6, USART_FLAG_TC) == RESET);//判断数据是否发送完
	USART_ClearFlag(USART6, USART_FLAG_TC);//Clears the USARTx's pending flags
	return ch;
}


#include "drv_water.h"
#include <stdio.h>
#include <string.h>

#define len 10  // 缓冲区长度，根据命令长度设置
char arr[len] = {0};  // 接收缓冲区
uint8_t data_len = 0;  // 已接收的字节数   

void USART6_IRQHandler(void)
{
	if(USART_GetITStatus(USART6, USART_IT_RXNE) != RESET)
	{		
        char data = USART_ReceiveData(USART6);  
        USART_ClearITPendingBit(USART6, USART_IT_RXNE);        
        //USART_SendData(USART6, data6);  
        if(data_len<len-1 && data != '\r' && data != '\n')
        {
            arr[data_len++]=data;
            arr[data_len]='\0';
        }
        // 检测到结束符（假设用回车\r或换行\n作为命令结束）
        else 
        {
            if(!strcmp(arr,"unled") )
            {
                drv_water1_close();
                printf("is off");   
            } 
             else if(!strcmp(arr,"led") )
            {
            drv_water1_open();
			printf("is on");
            } 
            data_len=0;
            memset(arr,0,10);            
        }  
        USART_SendData(USART6, data);     
        if(USART_GetFlagStatus(USART6, USART_FLAG_IDLE))
        {
            USART_ClearITPendingBit(USART6, USART_IT_IDLE);
            USART_ReceiveData(USART6); 
                                    
        }
    }
}
void drv_usart2_init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	USART_InitTypeDef USART_InitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
	
	
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;//复用模式
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOD, & GPIO_InitStruct);	

	GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART2);
	GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_USART2);
	
	USART_InitStruct.USART_BaudRate = 115200;
	USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	USART_InitStruct.USART_Parity = USART_Parity_No;//
	USART_InitStruct.USART_StopBits = USART_StopBits_1;
	USART_InitStruct.USART_WordLength = USART_WordLength_8b;
	USART_Init(USART2, &USART_InitStruct);
	
	USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启接收中断
	USART_ITConfig(USART2, USART_IT_IDLE, ENABLE);//开启空闲中断
	NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStruct);
	USART_ClearFlag(USART2, USART_FLAG_TC);//清空发送完成标志，否则会出现第一个字母丢失
	USART_Cmd(USART2, ENABLE);
}

